<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 15:20
 */
class Ticket extends Controller{
    /*
     * 票价管理
     * 数据列表
     */
    public function ListItem(){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        _Db::Validates($request_data);
        $where = '1=1';
        if(!empty($get_keyword->text)){
            $where .= ' and price = ' . $get_keyword->text;
        }
        $param_page = input('Page', 1);
        $param_pagesize = 10;
        $result = Db::table('ticket_price')
            ->field(
                [
                    'a.price_id'	=> 'Id',        //主键ID
                    'a.price'		=> 'Price',	    //票价
                    'a.oper_date'	=> 'Date',      //操作日期
                    'a.oper_id'		=> 'OperId',    //操作人Id
                    'b.account'     => 'OperName',     //操作人姓名
                ]
            )
            ->alias('a')
            ->join('staff b','a.oper_id = b.staff_id')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('a.oper_date desc')
            ->select();
        //echo Db::table('')->getLastSql();
        foreach ( $result as $key => $value){
            $result[$key]['Order'] = $key + 1;
        };
        $int_total_count = Db::table('ticket_price')->count();
        $data['tableData']=$result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $itemid = new RequestEntity('ItemId', 'ID', true, 0)
        ];
        _Db::Validates($request_data);
        $param_where = [
            'a.price_id' => $itemid->text,
        ];
        $result = Db::table('ticket_price')
            ->field(
                [
                    'a.price_id'	=> 'Id',        //主键ID
                    'a.price'		=> 'Price',	    //票价
                    'a.oper_date'	=> 'Date',      //操作日期
                    'a.oper_id'		=> 'OperId',    //操作人
                    'b.account'     => 'OperName',  //操作人
                ]
            )
            ->alias('a')
            ->join('staff b','a.oper_id = b.staff_id')
            ->where($param_where)
            ->find();
        return json_encode($result,true);
    }
    /*
     * 修改数据
     */
    public function ModifyItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
            $post_price = new RequestEntity('Price', '价格', true, 30),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $res = array(
            'price'		    => $post_price->text,	    //票价
            'oper_id'		=> $post_creatid->text,    //操作人
            'oper_date'	    => date('Y-m-d H:i:s',time()),      //操作日期
        );
        $param_where = [
            'price_id' => $post_itemid->text,
        ];
        $result = Db::table('ticket_price')
            ->where($param_where)
            ->update($res);
        if ($result){
            /*$field = [
                'userid'    => $GLOBALS["UserInfo"]['Id'],
                'login_ip'  => $GLOBALS["UserInfo"]['login_ip'],
                'type'      => 3,
                'content'   => '票价修改'
            ];
            _Db::log($field);*/
            $this->Item($param_where);
        };

    }
    /*
     * 保存数据
     */
    public function AddItem(){
        //获取请求参数
        $request_data = [
            $post_price = new RequestEntity('Price', '价格', true, 30),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $param_field = [
            'price'		    => $post_price->text,	    //票价
            'oper_id'		=> $post_creatid->text,    //操作人
            'oper_date'	    => date('Y-m-d H:i:s',time()),  //操作日期
        ];
        $res = Db::table('ticket_price')->insert($param_field);
        if ($res){
            /*$field = [
                'userid'    => $GLOBALS["UserInfo"]['Id'],
                'login_ip'  => $GLOBALS["UserInfo"]['login_ip'],
                'type'      => 1,
                'content'=> '票价信息添加'
            ];
            _Db::log($field);*/
            $this->ListItem();
        }
    }
    /*
     * 删除表数据
     */
    public function RemoveItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0)
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_where = [
            'price_id' => $post_itemid->text,
        ];
        $result = Db::table('ticket_price')
            ->where($param_where)
            ->delete();
        if ($result){
            /*$field = [
                'userid'    => $GLOBALS["UserInfo"]['Id'],
                'login_ip'  => $GLOBALS["UserInfo"]['login_ip'],
                'type'      => 3,
                'content'   => '票价信息删除'
            ];
            _Db::log($field);*/
            $this->ListItem();
        };
    }
    public function Item($param_where){
        $result = Db::table('ticket_price')
            ->field(
                [
                    'a.price_id'	=> 'Id',        //主键ID
                    'a.price'		=> 'Price',	    //票价
                    'a.oper_date'	=> 'Date',      //操作日期
                    'a.oper_id'		=> 'OperId',    //操作人Id
                    'b.account'     => 'OperName',  //操作人姓名
                ]
            )
            ->alias('a')
            ->join('staff b','a.oper_id = b.staff_id')
            ->where($param_where)
            ->order('a.oper_date desc')
            ->find();
        _Db::ResponseJson($result);
    }
}